﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

namespace Star_Wars
{
    class PlayerManager // класс игрока
    {
        public Sprite playerSprite;
        private int playerRadius = 20;
        private int playerLives = 0;
        private int playerPoints = 0;
        private int countOfAchievedGoals = 0;
        public bool Destroyed = false;

        public int PlayerPoints
        {
            get { return playerPoints; }
            set { playerPoints = value; }
        }

        public int PlayerLives
        {
            get { return playerLives; }
            set { playerLives = value; }
        }
        public int CountOfAchievedGoals
        {
            get { return countOfAchievedGoals; }
            set { countOfAchievedGoals = value; }
        }
        public void increasePlayerPoints()
        {
            playerPoints += 10;
        }
        public PlayerManager(Texture2D texture, Rectangle initialFrame, int frameCount, Rectangle screenBounds)
        {
            playerSprite = new Sprite(texture, new Vector2(500, 400), Vector2.Zero, initialFrame);
            for (int i = 1; i < frameCount; i++)
            {
                playerSprite.addFrame(new Rectangle(initialFrame.X + initialFrame.Width * i, initialFrame.Y, initialFrame.Width, initialFrame.Height));
            }
            playerSprite.CollisionRadius = playerRadius;
        }
        private void HandleKeyBoardInput(KeyboardState keyState) // управление
        {
            if (keyState.IsKeyDown(Keys.Left))
            {
                playerSprite.Rotation -= 0.07f;
            }
            if (keyState.IsKeyDown(Keys.Right))
            {
                playerSprite.Rotation += 0.07f;
            }
            if (keyState.IsKeyDown(Keys.Up))
            {
                playerSprite.Velocity = 3f * new Vector2((float)Math.Cos(playerSprite.Rotation - Math.PI / 2), (float)Math.Sin(playerSprite.Rotation - Math.PI / 2));
                playerSprite.Location += playerSprite.Velocity;
                playerSprite.Velocity = Vector2.Zero;
            }
        }
        private void isOutside(Game game) // выход за пределы игрового экрана
        {
            if (playerSprite.Location.X > game.Window.ClientBounds.Width) playerSprite.Location = new Vector2(0, playerSprite.Location.Y);
            if (playerSprite.Location.X < 0) playerSprite.Location = new Vector2(game.Window.ClientBounds.Width, playerSprite.Location.Y);
            if (playerSprite.Location.Y > game.Window.ClientBounds.Height) playerSprite.Location = new Vector2(playerSprite.Location.X, 0);
            if (playerSprite.Location.Y < 0) playerSprite.Location = new Vector2(playerSprite.Location.X, game.Window.ClientBounds.Height);
        }
        public void Update(Game1 game, GameTime gameTime)
        {
            if (!Destroyed)
            {
                HandleKeyBoardInput(Keyboard.GetState());
                isOutside(game);
                playerSprite.Update(gameTime);
            }
        }
        public void Draw(SpriteBatch spriteBatch)
        {
            if (!Destroyed) playerSprite.Draw(spriteBatch);
        }
    }
}
